﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using ChungNhanSinhVien.Entities;
using ChungNhanSinhVien.Infrastructures;

namespace ChungNhanSinhVien.Models
{
    public class YeuCauRepository
    {
        static Web_ChungNhanSinhVienEntities data;
        //public Web_ChungNhanSinhVienEntities getEntity()
        //{
        //    if (enties == null)
        //    {
        //        enties = new Web_ChungNhanSinhVienEntities();
        //    }
        //    return enties;
        //}

        public YeuCauRepository()
        {
            //if (data == null)
            data = new Web_ChungNhanSinhVienEntities();
        }

        public void ThemYeuCau(YeuCau yeucau)
        {
            data.AddToYeuCaus(yeucau);
            data.SaveChanges();
        }

        public bool KiemTraYeuCauDaGhiNhanHomNayChuaXuLy(YeuCauModel yeucau, string masv)
        {
            int cnID = int.Parse(yeucau.chungnhan);
            var datenow = DateTime.Now.Date;
            var kq = (from m in data.YeuCaus
                      where m.StudentID.Equals(masv)
                      && m.ChungNhanID.Value.Equals(cnID)
                      && m.NgayYeuCau.Value.Equals(datenow)
                      && !m.TrangThai.Equals("Xong")
                      select m).FirstOrDefault();
            if (kq != null)
                return true;
            return false;
        }

        public bool KiemTraYeuCauDangXuLy(YeuCauModel yc, string masv)
        {
            int cnID = int.Parse(yc.chungnhan);
            var kq = (from m in data.YeuCaus
                      where m.StudentID.Equals(masv)
                      && m.ChungNhanID.Value.Equals(cnID)
                      && (m.TrangThai.Equals("Đang xử lý") || m.TrangThai.Equals("Chờ xử lý"))
                      select m).FirstOrDefault();
            if (kq != null)
                return true;
            return false;
        }

        public List<YeuCau> LayYeuCausTheoMaSV(string studentID)
        {
            var list = new List<YeuCau>();
            list = (from m in data.YeuCaus
                    where m.StudentID.Equals(studentID)
                    orderby m.NgayYeuCau descending
                    select m).ToList();
            return list;
        }

        public List<YeuCau> LayYeuCausTheoMaSVTrangThai(string studentID, int trangthaiid)
        {
            TrangThaiYeuCauRepository trangthaiRepo = new TrangThaiYeuCauRepository();
            string trangthai = trangthaiRepo.LayTenTrangThaiTheoID(trangthaiid);
            var list = (from m in data.YeuCaus
                        where m.StudentID.Equals(studentID)
                        && m.TrangThai.Equals(trangthai)
                        select m).ToList();
            return list;
        }

        public void CapNhatTrangThaiYeuCausThanhDaGhiTra(List<YeuCau> yeucaus)
        {
            var list = from m in yeucaus select m;
            foreach (var item in list)
            {
                item.TrangThai = "Xong";
                item.ThoiDiemPhanHoiYeuCau = DateTime.Now;
            }
            data.SaveChanges();
        }

        public List<YeuCau> LayYeuCausTheoNgayDauCuoi(DateTime ngaydau, DateTime ngaycuoi, int soluong)
        {
            var list = new List<YeuCau>();
            list = (from m in data.YeuCaus
                    where (m.NgayYeuCau.Value.CompareTo(ngaydau) >= 0)
                    && (m.NgayYeuCau.Value.CompareTo(ngaycuoi) <= 0)
                    && m.TrangThai.Equals("Chờ xử lý")
                    && (m.ChungNhanID != null && m.ChungNhan.LoaiChungNhanID != null)
                    orderby m.NgayYeuCau
                    select m).Take(soluong).ToList();
            return list;
        }

        public YeuCau LayYeuCauTheoID(int id)
        {
            var kq = (from m in data.YeuCaus
                      where m.ID.Equals(id)
                      select m).FirstOrDefault();
            return kq;
        }

        public List<YeuCau> LayYeuCausMoiNhat(int soluong)
        {
            var list = new List<YeuCau>();
            list = (from m in data.YeuCaus
                    where m.TrangThai.Equals("Chờ xử lý")
                    && m.ChungNhanID != null
                    && m.ChungNhan.LoaiChungNhanID != null
                    orderby m.NgayYeuCau descending
                    select m).Take(soluong).ToList();
            return list;
        }

        public List<YeuCau> LayTatCaYeuCauMoiNhat()
        {
            var list = new List<YeuCau>();
            list = (from m in data.YeuCaus
                    where m.TrangThai.Equals("Chờ xử lý")
                    && m.ChungNhanID != null
                    && m.ChungNhan.LoaiChungNhanID != null
                    orderby m.NgayYeuCau descending
                    select m).ToList();
            return list;
        }

        public int DemSoLuongYeuCauTheoTrangThai(string trangthai)
        {
            var count = (from m in data.YeuCaus
                         where m.TrangThai.Equals(trangthai)
                         && m.ChungNhanID != null
                         && m.ChungNhan.LoaiChungNhanID != null
                         select m).Count();
            return count;
        }

        public void ChuyenTrangThaiChoYeuCauCuaDSSV(List<YeuCau> yeucaus, string trangthai)
        {
            foreach (var y in yeucaus)
            {
                y.TrangThai = trangthai;
                y.ThoiDiemPhanHoiYeuCau = DateTime.Now;
            }
            data.SaveChanges();
        }

        public void ChuyenTrangThaiChoYeuCauCuaSV(YeuCau yeucau, string trangthai)
        {
            yeucau.TrangThai = trangthai;
            yeucau.ThoiDiemPhanHoiYeuCau = DateTime.Now;
            data.SaveChanges();
        }

        public void CapNhatYeuCau(YeuCau yc)
        {
            //var kq = (from m in CNSVStatic.getEntity().YeuCaus
            //          where m.StudentID.Equals(yc.StudentID)
            //              //&& m.ChungNhan.LoaiChungNhanID.Value.Equals(yc.ChungNhan.LoaiChungNhanID.Value)
            //          && m.ChungNhanID.Value.Equals(yc.ChungNhanID.Value)
            //          && m.TrangThai.Equals("Đang xử lý")
            //          select m).FirstOrDefault();            
            var kq = (from m in data.YeuCaus
                      where m.ID.Equals(yc.ID)
                      && m.TrangThai.Equals("Đang xử lý")
                      select m).FirstOrDefault();
            if (kq != null)
            {
                kq.TrangThai = yc.TrangThai;
                kq.ThoiDiemPhanHoiYeuCau = DateTime.Now;
                kq.GhiChu = yc.GhiChu;
                data.SaveChanges();
                new NhatKyXuLyYeuCauRepository().Them(kq.ID, "Nhập liệu yêu cầu sau xử lý");
            }
        }



        public bool Xoa(int id)
        {
            var kq = (from m in data.YeuCaus
                      where m.ID.Equals(id)
                      select m).FirstOrDefault();
            if (kq != null)
            {
                data.DeleteObject(kq);
                data.SaveChanges();
                return true;
            }
            return false;
        }
    }
}